package com.ruoyi.store.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.store.bo.HmSystemAttachmentAddBo;
import com.ruoyi.store.bo.HmSystemAttachmentEditBo;
import com.ruoyi.store.bo.HmSystemAttachmentQueryBo;
import com.ruoyi.store.domain.HmSystemAttachment;
import com.ruoyi.store.mapper.HmSystemAttachmentMapper;
import com.ruoyi.store.service.AsyncService;
import com.ruoyi.store.service.IHmSystemAttachmentService;
import com.ruoyi.store.vo.FileResultVo;
import com.ruoyi.store.vo.HmSystemAttachmentVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 素材管理Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-03
 */
@Service
public class HmSystemAttachmentServiceImpl extends ServicePlusImpl<HmSystemAttachmentMapper, HmSystemAttachment> implements IHmSystemAttachmentService {

	@Autowired
	private IHmSystemAttachmentService systemConfigService;

	@Autowired
	private AsyncService asyncService;

	@Override
	public HmSystemAttachmentVo queryById(Integer attId){
		return getVoById(attId, HmSystemAttachmentVo.class);
	}

	@Override
	public TableDataInfo<HmSystemAttachmentVo> queryPageList(HmSystemAttachmentQueryBo bo) {
		if(bo.getPid()==0){
			bo.setPid(null);
		}
		PagePlus<HmSystemAttachment, HmSystemAttachmentVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), HmSystemAttachmentVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<HmSystemAttachmentVo> queryList(HmSystemAttachmentQueryBo bo) {
		return listVo(buildQueryWrapper(bo), HmSystemAttachmentVo.class);
	}

	private LambdaQueryWrapper<HmSystemAttachment> buildQueryWrapper(HmSystemAttachmentQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<HmSystemAttachment> lqw = Wrappers.lambdaQuery();
		lqw.like(StrUtil.isNotBlank(bo.getName()), HmSystemAttachment::getName, bo.getName());
		lqw.eq(StrUtil.isNotBlank(bo.getAttDir()), HmSystemAttachment::getAttDir, bo.getAttDir());
		lqw.eq(StrUtil.isNotBlank(bo.getSattDir()), HmSystemAttachment::getSattDir, bo.getSattDir());
		lqw.eq(StrUtil.isNotBlank(bo.getAttSize()), HmSystemAttachment::getAttSize, bo.getAttSize());
		lqw.eq(StrUtil.isNotBlank(bo.getAttType()), HmSystemAttachment::getAttType, bo.getAttType());
		lqw.eq(bo.getPid() != null, HmSystemAttachment::getPid, bo.getPid());
		lqw.eq(bo.getImageType() != null, HmSystemAttachment::getImageType, bo.getImageType());
		lqw.eq(bo.getType()!=null,HmSystemAttachment::getType,bo.getType());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(HmSystemAttachmentAddBo bo) {
		HmSystemAttachment add = BeanUtil.toBean(bo, HmSystemAttachment.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(HmSystemAttachmentEditBo bo) {
		HmSystemAttachment update = BeanUtil.toBean(bo, HmSystemAttachment.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(HmSystemAttachment entity){
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
		if(isValid){
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}

	/**
	 * 查询本地文件，同步到云服务
	 */
	@Override
	@Transactional
	public void async() {

	}

	/**
	 * 保存文件
	 * @param file FileResultVo 文件信息
	 * @param pid Integer
	 */
	@Override
	public void create(FileResultVo file, Integer pid) {
		HmSystemAttachment systemAttachment = new HmSystemAttachment();
		systemAttachment.setName(file.getFileName());
		systemAttachment.setAttDir(file.getUrl());
		systemAttachment.setSattDir(file.getUrl());
		systemAttachment.setAttSize(file.getFileSize().toString());
		systemAttachment.setAttType(file.getType());
		systemAttachment.setImageType(1);   //图片上传类型 1本地 2七牛云 3OSS 4COS, 默认本地，任务轮询数据库放入云服务
		systemAttachment.setAttDir(file.getServerPath()); // 服务器上存储的绝对地址， 上传到云的时候使用
		systemAttachment.setPid(pid);
		save(systemAttachment);
	}
	/**
	 * 清除 cdn url， 在保存数据的时候使用
	 * @param path String 文件路径
	 * @return String
	 */
	@Override
	public String clearPrefix(String path) {
		if(StringUtils.isBlank(path)){
			return path;
		}
		Constants.CND_URL = asyncService.getCurrentBaseUrl();
		if(path.contains(getCdnUrl() + "/")){
			return path.replace(getCdnUrl() , "");
		}

		return path;
	}
	/**
	 * 获取cdn url
	 * @return String
	 */
	private String getCdnUrl() {
		return asyncService.getCurrentBaseUrl();
	}
	/**
	 * 同步到云服务， 更新图片上传类型
	 * @param attId Integer 主键id
	 * @param type int 图片上传类型 1本地 2七牛云 3OSS 4COS
	 */
	@Override
	public void updateCloudType(Integer attId, int type) {
		HmSystemAttachment systemAttachment = new HmSystemAttachment();
		systemAttachment.setAttId(attId);
		systemAttachment.setImageType(type);
		updateById(systemAttachment);
	}

}
